package com.dnjn.happiness.biz.controller.admin;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.biz.domain.TrainingRecord;
import com.dnjn.happiness.biz.domain.TrainingUserVO;
import com.dnjn.happiness.biz.service.ITrainingRecordService;
import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.utils.StringUtils;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import com.dnjn.happiness.system.api.RemoteDeptService;
import com.dnjn.happiness.system.api.RemoteRoleService;
import com.dnjn.happiness.system.api.domain.SysDept;
import com.dnjn.happiness.system.api.domain.SysUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 培训签到记录Controller
 *
 * @author happiness
 * @date 2023-11-04
 */
@Tag(name = "admin biz record 后台-培训签到记录", description = "后台培训签到记录")
@RestController
@RequestMapping("/admin/record")
public class AdminTrainingRecordController extends BaseController {
    @Autowired
    private ITrainingRecordService trainingRecordService;
    @Autowired
    private RemoteDeptService deptService;




    @RequiresPermissions("biz:record:query")
    @Operation(summary = "后台培训详情 数据图表-echarts")
    @GetMapping("/miniProgramTrainingDetailsDataChartsAdmin/{trainingId}")
    public AjaxResult selectUserByLeaderIdAdmin(@PathVariable("trainingId") Long trainingId) {
        List<TrainingRecord> list = trainingRecordService.lambdaQuery().eq(TrainingRecord::getTrainingId, trainingId).list();
        int trainingRecordStatusCountY = 0;
        int trainingRecordStatusCountN = 0;
        int trainingRecordStatusResult = 0;
        int total = list.size();
        for (TrainingRecord record : list) {
            if (ObjectUtils.isNotEmpty(record.getTrainingRecordStatus()) && record.getTrainingRecordStatus().equals("Y")) {
                trainingRecordStatusCountY += 1;
            } else {
                trainingRecordStatusCountN += 1;
            }
            if (ObjectUtils.isNotNull(record.getResult())) {
                trainingRecordStatusResult += 1;
            }
        }
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("trainingRecordStatusCountY", trainingRecordStatusCountY);
        ajaxResult.put("trainingRecordStatusCountN", trainingRecordStatusCountN);
        ajaxResult.put("trainingRecordStatusResult", trainingRecordStatusResult);
        ajaxResult.put("total", total);
        return ajaxResult;

    }

    // 培训反馈
    @RequiresPermissions("biz:record:add")
    @Operation(summary = "添加培训反馈")
    @PutMapping("/addTrainingRecordResult")
    public AjaxResult addTrainingRecordResult(@Parameter(description = "培训记录id") @RequestParam String trainingRecordId, @Parameter(description = "反馈") @RequestParam String result) {

        return success(trainingRecordService.lambdaUpdate().eq(TrainingRecord::getTrainingRecordId, trainingRecordId).set(TrainingRecord::getResult, result).update());
    }

    @Deprecated
    @RequiresPermissions("biz:record:query")
    @Operation(summary = "查看培训反馈")
    @GetMapping("/selectTrainingRecordResult")
    public AjaxResult selectTrainingRecordResult(@Parameter(description = "培训记录id") @RequestParam Long trainingRecordId) {

        TrainingRecord one = trainingRecordService.lambdaQuery().eq(TrainingRecord::getTrainingRecordId, trainingRecordId).one();
        return success(one);
    }


    //
    //点击切换
    @Deprecated
    @RequiresPermissions("biz:record:query")
    @Operation(summary = "查看反馈")
    @GetMapping("/ForTrainingDetailsSeeFeedback")
    public AjaxResult forTrainingDetailsSeeFeedback(@RequestParam Long trainingId) {
        List<SysUser> userList = trainingRecordService.forTrainingDetailsSeeFeedback(trainingId);
        return success(userList);
    }


    @RequiresPermissions("biz:record:query")
    @Operation(summary = "后台-查看反馈-11.30 更改新需求-不是二层树结构的-带分页", description = "role:2高亮;1:普通,默认1普通")
    @GetMapping("/goToTheTrainingDetailsViewAdmin")
    public AjaxResult goToTheTrainingDetailsViewAdmin(@RequestParam Long trainingId, @RequestParam String status, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page<TrainingRecord> page = Page.of(current, size);
        Page<TrainingRecord> userList = trainingRecordService.goToTheTrainingDetailsView(page, status, trainingId);
        List<TrainingRecord> records = userList.getRecords();
        trainingRecordService.getInfo(records);

        return success(userList);

    }

    //点击主管查看里边详情
    @Deprecated
    @Operation(summary = "查看各个主管下边的反馈情况")
    @GetMapping("/ForTrainingDetailsSeeFeedbackByTrainingIdAndLeaderId")
    public AjaxResult forTrainingDetailsSeeFeedbackByTrainingIdAndLeaderId(@RequestParam Long trainingId, @RequestParam Long leaderId) {


        List<TrainingRecord> trainingRecords = trainingRecordService.forTrainingDetailsSeeFeedbackByTrainingIdAndLeaderId(trainingId, leaderId);

        int total = trainingRecords.size();

        ArrayList<TrainingRecord> listY = new ArrayList<>();
        ArrayList<TrainingRecord> listN = new ArrayList<>();

        trainingRecords.stream().forEach(trainingRecord -> {
            String trainingRecordStatus = trainingRecord.getTrainingRecordStatus();
            if (trainingRecordStatus.equals("Y")) {
                listY.add(trainingRecord);
            } else {
                listN.add(trainingRecord);
            }
        });

        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("total", total);
        ajaxResult.put("notInvolved", listN);
        ajaxResult.put("participated", listY);
        return AjaxResult.success(ajaxResult);

    }

    //  点击筛选 部门列表
    @RequiresPermissions("biz:record:query")
    @Operation(summary = "点击筛选")
    @PostMapping("/screeningTraining")
    public AjaxResult list(SysDept dept) {
        List<SysDept> depts = new ArrayList<>();
        R<List<SysDept>> result = deptService.selectDeptList(dept, SecurityConstants.INNER);
        if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
            depts = result.getData();
        }
        return success(depts);
    }

    // 提交筛选
    @RequiresPermissions("biz:record:query")
    @Operation(summary = "提交筛选")
    @PostMapping("/screeningTrainingSubmit")
    public AjaxResult screeningTrainingSubmit(Long trainingId, @RequestParam(required = false) Long deptId, @RequestParam(required = false) String nickName) {
        Assert.notNull(trainingId, "trainingId必传");
        List<TrainingUserVO> trainingUserVOList = trainingRecordService.screeningTrainingSubmit(trainingId, deptId, nickName);
        trainingUserVOList.stream().forEach(trainingUserVO -> {
            Long deptId1 = trainingUserVO.getDeptId();
            SysDept sysDept = new SysDept();
            R<SysDept> result = deptService.getInfoById(deptId1, SecurityConstants.INNER);
            if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
                sysDept = result.getData();
            }
            trainingUserVO.setSysDept(sysDept);


        });
        return AjaxResult.success(trainingUserVOList);
    }

}
